package com.liliudong.template.api.admin.handler;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.liliudong.template.common.constant.EntityFieldConst;
import com.liliudong.template.common.constant.SaSessionConst;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * 简单元对象处理程序
 *
 * @author ldli5
 * @date 2024/06/19
 */
public class SimpleMetaObjectHandler implements MetaObjectHandler {

	@Override
	public void insertFill(MetaObject metaObject) {
		long userId = StpUtil.getLoginIdAsLong();
		String deptNo = StpUtil.getTokenSession().getModel(SaSessionConst.DEPT_NO, String.class);
		this.strictInsertFill(metaObject, EntityFieldConst.CREATED_AT, LocalDateTime.class, LocalDateTime.now());
		this.strictInsertFill(metaObject, EntityFieldConst.CREATED_BY, Long.class, userId);
		this.strictInsertFill(metaObject, EntityFieldConst.UPDATED_AT, LocalDateTime.class, LocalDateTime.now());
		this.strictInsertFill(metaObject, EntityFieldConst.UPDATED_BY, Long.class, userId);
		this.strictInsertFill(metaObject, EntityFieldConst.CREATED_FROM, String.class, deptNo);
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		long userId = StpUtil.getLoginIdAsLong();
		this.strictInsertFill(metaObject, EntityFieldConst.UPDATED_AT, LocalDateTime.class, LocalDateTime.now());
		this.strictInsertFill(metaObject, EntityFieldConst.UPDATED_BY, Long.class, userId);
	}
}
